﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Schema;

namespace LinqToXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement exemplos = XElement.Load(Environment.CurrentDirectory + @"\Exemplos.xml");


            WriteExampleHeading("Exemplo - Bares em Lisboa");

            var baresEmLisboa =
            from bar in exemplos.Descendants("Bar")
            where bar.Element("Localidade").Value == "Lisboa"
            orderby bar.Element("Nome").Value
            select bar.Element("Nome").Value;

            WriteResult<string>(baresEmLisboa);



            //WriteExampleHeading("Exemplo - Validação de xsd");

            //XmlSchemaSet schemas = new XmlSchemaSet();
            //schemas.Add("", XmlReader.Create(Environment.CurrentDirectory + @"\Schema.xsd"));

            //XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Exemplos.xml");

            //Console.WriteLine("Validating Exemplos");
            //bool errors = false;
            //doc.Validate(schemas, (o, e) =>
            //{
            //    Console.WriteLine("{0}", e.Message);
            //    errors = true;
            //});
            //Console.WriteLine("Exemplos {0}", errors ? " Não Validou" : " Validou");






            //WriteExampleHeading("Exemplo - Criação de XML (funcionalmente)");

            //XElement element =
            //    new XElement("Root",
            //        new XElement("Bares",
            //            new XElement("Bar",
            //                new XElement("Id", 1),
            //                new XElement("Nome", "Kapital"),
            //                new XElement("Localidade", "Lisboa"),
            //                new XElement("Entrada", 20m)),
            //            new XElement("Bar",
            //                new XElement("Id", 1),
            //                new XElement("Nome", "Touareg"),
            //                new XElement("Localidade", "Odivela"),
            //                new XElement("Entrada", 6m)),
            //            new XElement("Bar",
            //                new XElement("Id", 1),
            //                new XElement("Nome", "Quadrados"),
            //                new XElement("Localidade", "Bobadela"),
            //                new XElement("Entrada", 0))
            //                ));

            //Console.WriteLine("Xml : " + Environment.NewLine + element.ToString());





            Console.ReadLine();
        }

        #region Helper Methods

        private static void WriteExampleHeading(string descricaoExemplo)
        {
            if (first)
                first = false;
            else
                Console.WriteLine(Environment.NewLine);
            Console.WriteLine(descricaoExemplo + Environment.NewLine);
        }
        private static void WriteResult<T>(IEnumerable<T> resultados)
        {
            foreach (T resultado in resultados)
            {
                WriteObject(resultado);
            }
        }
        private static void WriteObject(object objectToWrite)
        {
            XElement obj = objectToWrite as XElement;
            if (obj != null)
            {
                Console.WriteLine(obj.ToString());
                return;
            }

            Console.WriteLine(objectToWrite.ToString());
        }

        static bool first = true;

        #endregion

    }
}
